import React, { useCallback, useEffect, useState } from 'react';
import useBasisCash from './useBasisCash';
import config from '../config';
import {CommonTvl} from "../common/common";

const useTotalTvl = ():any => {
  const [stat, setStat] = useState(0);
  const basisCash = useBasisCash();

  const getTotalTvl = useCallback(async () => {
   setStat(Object.values(CommonTvl).reduce((a,b)=>{
     return a+b;
   }))
  }, [basisCash]);

  useEffect(() => {
    getTotalTvl().catch((err) => console.error(`Failed to fetch SNB price: useCashStats ${err.stack}`));
    const refreshInterval = setInterval(getTotalTvl, config.refreshInterval);
    return () => clearInterval(refreshInterval);
  }, [basisCash]);

  return stat;
};

export default useTotalTvl;
